<?php
namespace BoojasrFramework;


use BoojasrFramework\Response\Response;
use BoojasrFramework\ServicesContainer\ContainerInterface;
use BoojasrFramework\ServicesContainer\Kernel as ServicesContainerKernel;
use BoojasrFramework\ServicesContainer\Request;
use BoojasrFramework\ServicesContainer\TerminableInterface;
use Symfony\Component\Routing\Route;

class Kernel implements KernelInterface
{
    const VERSION = "v1.0.0";

    private $env;

    private $booted;

    public function __construct($env="prod")
    {
        $this->env = $env;
    }

    /**
     * @return mixed
     */
    public function getEnv():?string
    {
        return $this->env;
    }

    /**
     * @param Request $request
     * @return Response
     */
    public function handle(Request $request):Response
    {
        $kernel = $this->getServiceContainerKernel();
        $response = $kernel->handle($request);
        $this->booted = true;
        return $response;
    }

    public function getServiceContainerKernel(){
        static $scKernel;
        if (!$scKernel){
            $scKernel = ServicesContainerKernel::instance();
            $scKernel->setEnv($this->getEnv());
        }
        return $scKernel;
    }

    public function boot()
    {
        $kernel = $this->getServiceContainerKernel();
        $kernel->boot();
        $container = $kernel->getContainer();
        $container->set("kernel",$this);
        $this->booted = true;
    }

    public function getContainer():ContainerInterface
    {
        return $this->getServiceContainerKernel()->getContainer();
    }

    /**
     * @return Route[]
     */
    public static function getRouteConfigs(){
        static $routeConfigs;
        if (!$routeConfigs){
            // 路由配置注册
            $routeConfigs = require_once dirname(__DIR__)."/config/routes.php";
        }
        return $routeConfigs;
    }

    /**
     * @return mixed|null
     */
    public static function getDatabaseConfig(){
        static $r;
        if ($r){
            return $r;
        }
        $config_file = dirname(__DIR__)."/config/database.php";
        if (file_exists($config_file)){
            $r = require_once $config_file;
            return $r;
        }
        return null;
    }

    /**
     * @return mixed|null
     */
    public static function getConfig(){
        static $r;
        if ($r){
            return $r;
        }
        $config_file = self::getProjectPath()."config/config.php";
        if (file_exists($config_file)){
            $r = require_once $config_file;
            return $r;
        }
        return null;
    }

    public static function getProjectPath(){
        return dirname(__DIR__)."/";
    }


    public function terminate(Request $request, Response $response){
        if (false === $this->booted) {
            return;
        }

        if ($this->getServiceContainerKernel()->getHttpKernel() instanceof TerminableInterface) {
            $this->getServiceContainerKernel()->getHttpKernel()->terminate($request, $response);
        }
    }


}
